package com.yijiankang.threaddemo.example.sync;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class Synchronized2 {
    

    //修饰类 代码都被 syn代码包围  通修饰静态方法 输出结果一样
    public static void test1(int j){
        synchronized (Synchronized2.class){
            for(int i=0;i<10;i++){
                log.info("test-1:{}--{}",j,i);
            }
        }
    }

    //修饰静态方法
    public static synchronized  void test2(int j){
        for(int i=0;i<10;i++){
            log.info("test-2:{}--{}",j,i);
        }
    }

    public static void main(String[] args) {
        Synchronized2 synchronized1 = new Synchronized2();
        Synchronized2 synchronized2 = new Synchronized2();
        ExecutorService executorService = Executors.newCachedThreadPool();
       /* executorService.execute(()->{
            synchronized1.test1(1);
        });

        executorService.execute(()->{
            synchronized2.test1(2);
        });*/

        executorService.execute(()->{
            synchronized1.test1(1);
        });

        executorService.execute(()->{
            synchronized2.test1(2);
        });
    }
}
